<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>页面的模板</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/static/adminlte/bower_components/bootstrap/dist/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/static/adminlte/bower_components/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="/static/adminlte/bower_components/Ionicons/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/static/adminlte/dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="/static/adminlte/dist/css/skins/_all-skins.min.css">


</head>

<body class="hold-transition skin-purple sidebar-mini">
<div class="wrapper">

    <jsp:include page="../common/header.jsp"></jsp:include>
    <jsp:include page="../common/mune.jsp"></jsp:include>



    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                学生列表
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li class="active">学生管理</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content container-fluid">


            <div class="row">
                <div class="col-md-12">
                    <div class="box">
                        <div class="box-header">
                            <button class="btn btn-danger btn-sm" id="batchDel">批量删除</button>
                        </div>
                        <div class="box-body">
                            <table class="table table-bordered" id="table1">

                            </table>
                        </div>

                    </div>
                    <!-- /.box -->

                </div>
            </div>


        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->




</div>
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 3 -->
<script src="/static/adminlte/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="/static/adminlte/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="/static/adminlte/dist/js/adminlte.min.js"></script>
  <!--  https://www.layuiweb.com/doc/modules/layer.html -->
<script src="/static/adminlte/bower_components/layer/layer.js"></script>

<script src="/static/adminlte/bower_components/bootstrap-table/bootstrap-table.js" ></script>
<script src="/static/adminlte/bower_components/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<%--  ajax  请求 加载 实体数据，通过表格展示，借助一些 前端表格插件， 推荐一个 bootstrap-table ，基于bootstrap 样式，相对比较吻合。--%>
<script>
    $(function () {
        window.operateEvents={
            // 更改操作的方法
            'click .btn_update': function (e, value, row, index) {
                var id = row.id;
                console.log("更改: "+id);
            },
            //删除 单行/
            'click .btn_del': function (e, value, row, index) {
                var id = row.id;
                layer.confirm('确认删除【'+row.name +" 】? " , {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        type: "POST",
                        url: "/stu/del.do",
                        data: "id="+id,
                        dataType:"json",
                        success: function(obj){
                            if(obj.success){
                                // 删除成功后，自动刷新
                                $('#table1').bootstrapTable('refresh');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        }

        $('#table1').bootstrapTable('destroy').bootstrapTable({
            method: 'post',
            contentType:"application/x-www-form-urlencoded",
            url: "/stu/loadStudents.do",
            striped: true,
            dataField: "records",
            totalField: "count",
            pageNumber: 1,
            pageSize: 10,
            pagination: true,
            queryParamsType: 'limit',
            //search: true,
            queryParams: function (params) {
                return {
                    pageSize: params.limit,
                    pageNo: params.offset / params.limit + 1
                }
            },
            sidePagination: 'server',
            pageList: "[10, 25, 50, 100, all]",
            showRefresh: true,
            clickToSelect: true,
            toolbarAlign: 'left',
            buttonsAlign: 'right',
            toolbar: '#toolbar',
            showColumns: false,
            paginationLoop: false,
            columns: [
                {
                    title: 'ID',
                    field: 'id',
                    visible: false,
                    width: 160
                },
                {
                    //  title: '全选/反选',
                    checkbox: true
                }
                ,
                {
                    title: '姓名',
                    field: 'name',
                    visible: true,
                    width: 160
                },
                {
                    title: '性别',
                    field: 'sex',
                    visible: true,
                    width: 50,
                    align:"center",
                    formatter:function (value, row, index) {
                        return value==1?"男":"女";
                    }
                },
                {
                    title: '学号',
                    field: 'stuNo',
                    visible: true,
                    width: 160
                },
                {
                    title: '注册时间',
                    field: 'regTimeStr',
                    visible: true,
                    width: 160
                },
                {
                    title: '操作',
                    width: 80,
                    align: 'center',
                    formatter:function (value, row, index) {
                        return [
                            '<button  type="button" class="btn btn-danger btn-xs btn_del">',
                            '删除' ,
                            '</button>',
                            ' <button  type="button" class="btn btn-primary btn-xs btn_update">',
                            '更改' ,
                            '</button>'
                        ].join('')
                    },
                    events:window.operateEvents
                }
            ],
            locale: 'zh-CN',
            responseHandler: function (data) {
                return data['data'];
            }

        });


    //    批量删除

        $("#batchDel").click(function () {
            var rows =  $('#table1').bootstrapTable('getSelections');
            var ids= $.map(rows,function (row) {
                return row.id;
            });

            if(ids.length>0){ 
                layer.confirm("确认删除", {icon: 3, title:'删除操作'}, function(index){
                    var paramData = "";
                    for (var i = 0; i < ids.length; i++) {
                        paramData = paramData+ "id="+ids[i] +"&";
                    }
                    $.ajax({
                        type: "post",
                        url: "/stu/del.do",
                        data:paramData,
                        success: function(msg){
                            if(msg["success"]){
                                $('#table1').bootstrapTable('refresh');
                                layer.msg("批量操作成功",{time:700},function () {
                                    // $('#table1').bootstrapTable('refresh');
                                     $('#table1').bootstrapTable('remove', {
                                         field: 'id',
                                          values: ids
                                     });
                                });

                            }
                        }
                    });
                    layer.close(index);
                });
            }else {
                layer.msg("未选中行",{time:700});
            }
        });
    })
</script>
</body>
</html>
